<template>
<div class="xtx-goods-page" v-if="goods">
    <div v-if="loading" class="loading"></div>
    <div class="container" v-else>
    <!-- 面包屑 -->
        <XtxBread>
            <XtxBreadItem to="/">首页</XtxBreadItem>
            <XtxBreadItem :to="`/category/${goods_2.id}`">{{ goods_2.name }}</XtxBreadItem>
            <XtxBreadItem :to="`/category/sub/${goods_3.id}`">{{ goods_3.name }}</XtxBreadItem>
            <XtxBreadItem>{{ Name }}</XtxBreadItem>
        </XtxBread>
    <!-- 商品信息 -->
        <div class="goods-info">
            <div class="media">
                <GoodsImage :images="goods.mainPictures"></GoodsImage>
                <GoodsSales></GoodsSales>
            </div>
            <div class="spec">
                <!-- 名字区组件 -->
                <GoodsName :goods="goods"></GoodsName>
                <!-- 规格组件 -->
                <GoodsSku :goods="goods" @change="changeSku"></GoodsSku>
                <!-- 数量选择组件 -->
                <XtxNumbox label="数量" v-model="num" :max="goods.inventory"></XtxNumbox>
            </div>
        </div>
    <!-- 商品推荐 -->
        <GoodsRelevant></GoodsRelevant>
    <!-- 商品详情 -->
        <div class="goods-footer">
            <div class="goods-article">
            <!-- 商品 + 评价 -->
                <div class="goods-tabs"></div>
            <!-- 注意事项 -->
                <div class="goods-warn"></div>
            </div>
            <!-- 24热榜 + 专题推荐 -->
            <div class="goods-aside"></div>
        </div>
    </div>
</div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getGoods } from '@/api/product'
import { useRoute } from 'vue-router'
import XtxBread from "@/components/library/xtx-bread.vue";
import XtxBreadItem from "@/components/library/xtx-bread-item.vue";
import GoodsRelevant from "@/views/goods/components/goods-relevant.vue";
import GoodsImage from "@/views/goods/components/goods-image.vue";
import GoodsSales from "@/views/goods/components/goods-sales.vue";
import GoodsName from "@/views/goods/components/goods-name.vue";
import GoodsSku from "@/views/goods/components/goods-sku.vue";
import XtxNumbox from "@/components/library/xtx-numbox.vue";

onMounted(() => {
    useGoods()
})
 const loading = ref(false)
 const Name = ref("")
 const goods = ref(null)
 const goods_2 = ref("")
 const goods_3 = ref("")
 const route = useRoute()

// 选择的数量
const num = ref(1)

// 获取商品详情
 const useGoods = async () => {
    loading.value = true
    const { result } = await getGoods(route.params.id)
    goods.value = result
    Name.value = result.name
    goods_2.value = result.categories[0]
    goods_3.value = result.categories[1]
    loading.value = false
 }

 // sku改变时候触发
const changeSku = (sku) => {
    if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
    }
}
</script>

<style scoped lang="less">
@import '@/assets/styles/variables';

.goods-info {
  display: flex;
  min-height: 600px;
  background: #fff;
  .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
  }
  .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

.loading {
    height: 290px;
    width: 100%;
    background: url(../../assets/images/loading.gif) no-repeat center;
}
</style>
